/*
 * Copyright 2012 The Netty Project
 *
 * The Netty Project licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package com.doeis.base;

import java.net.SocketAddress;
import java.util.Date;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;

/**
 * @author lilinfeng
 * @author tianbx
 * @date 2014年2月14日
 * @version 1.0
 */
public class TimeServerHandler extends ChannelHandlerAdapter {

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg)
			throws Exception {
		System.err.println(new Date()+"TimeServerHandler >> channelRead ");
		ByteBuf buf = (ByteBuf) msg;
		byte[] req = new byte[buf.readableBytes()];
		buf.readBytes(req);
		String body = new String(req, "UTF-8");
		System.out.println("The time server receive order : " + body);
		String currentTime = "QUERY TIME ORDER".equalsIgnoreCase(body) ? new java.util.Date(
				System.currentTimeMillis()).toString() : "BAD ORDER";
		ByteBuf resp = Unpooled.copiedBuffer(currentTime.getBytes());
		ctx.write(resp);
	}

	
	
	@Override
	public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
		System.err.println(" TimeServerHandler >> handlerAdded ");
		super.handlerAdded(ctx);
	}



	@Override
	public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
		System.err.println(" TimeServerHandler >> handlerRemoved ");
		super.handlerRemoved(ctx);
	}



	@Override
	public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
		System.err.println(" TimeServerHandler >> channelRegistered ");
		super.channelRegistered(ctx);
	}



	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		System.err.println(" TimeServerHandler >> channelActive ");
		super.channelActive(ctx);
	}



	@Override
	public void channelInactive(ChannelHandlerContext ctx) throws Exception {
		System.err.println(" TimeServerHandler >> channelInactive ");
		super.channelInactive(ctx);
	}



	@Override
	public void userEventTriggered(ChannelHandlerContext ctx, Object evt)
			throws Exception {
		System.err.println(" TimeServerHandler >> userEventTriggered ");
		super.userEventTriggered(ctx, evt);
	}



	@Override
	public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress,
			SocketAddress localAddress, ChannelPromise promise)
			throws Exception {
		System.err.println(" TimeServerHandler >> connect ");
		super.connect(ctx, remoteAddress, localAddress, promise);
	}



	@Override
	public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise)
			throws Exception {
		System.err.println(" TimeServerHandler >> disconnect ");
		super.disconnect(ctx, promise);
	}



	@Override
	public void close(ChannelHandlerContext ctx, ChannelPromise promise)
			throws Exception {
		System.err.println(" TimeServerHandler >> close ");
		super.close(ctx, promise);
	}



	@Override
	public void read(ChannelHandlerContext ctx) throws Exception {
		System.err.println(" TimeServerHandler >> read ");
		super.read(ctx);
	}



	@Override
	public void write(ChannelHandlerContext ctx, Object msg,
			ChannelPromise promise) throws Exception {
		System.err.println(" TimeServerHandler >> write ");
		super.write(ctx, msg, promise);
	}



	@Override
	public void flush(ChannelHandlerContext ctx) throws Exception {
		System.err.println(" TimeServerHandler >> flush ");
		super.flush(ctx);
	}


  
	@Override
	public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
		System.err.println(" TimeServerHandler >> channelReadComplete ");
		ctx.flush();
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
		System.err.println(" TimeServerHandler >> exceptionCaught ");
		ctx.close();
	}
}
